﻿using System;
using System.ComponentModel;
using System.Windows.Input;
using QueryBuilder.Core.Common;

namespace QueryBuilder.Core.Visualization.Commands
{
    public class DelegateCommand<T> : ICommand, INotifyPropertyChanged
    {
        /// <summary>
        /// deleget to canExecute when method CanExecute is called
        /// </summary>
        private readonly Predicate<T> canExecuteMethod;

        /// <summary>
        /// delegate to execute when method Execute is called
        /// </summary>
        private readonly Action<T> executeMethod;

        /// <summary>
        ///  value indicating whether the command is active
        /// </summary>
        private bool isActive;

        /// <summary>
        /// Initializes a new instance of the DelegateCommand class
        /// </summary>
        /// <param name="executeMethod">
        /// execute method
        /// </param>
        public DelegateCommand(Action<T> executeMethod)
            : this(executeMethod, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the DelegateCommand class
        /// </summary>
        /// <param name="executeMethod">
        /// delegate to execute when method Execute is called
        /// </param>
        /// <param name="canExecuteMethod">
        /// deleget to canExecute when method CanExecute is called
        /// </param>
        public DelegateCommand(Action<T> executeMethod, Predicate<T> canExecuteMethod)
        {
            Validator.NotNull(executeMethod);
            this.executeMethod = executeMethod;
            this.canExecuteMethod = canExecuteMethod ?? ((parameter) => true);
        }

        /// <summary>
        /// Gets or sets a value indicating whether the command is active.
        /// </summary>
        public bool IsActive
        {
            get { return this.isActive; }

            set
            {
                this.isActive = value;
                this.PropertyChanged.Raise(() => this.IsActive);
            }
        }

        /// <summary>
        /// Raise for to check if the command can execute.
        /// </summary>
        /// <param name="parameter">
        /// params of command
        /// </param>
        /// <returns>
        /// returns true if the command can execute
        /// </returns>
        bool ICommand.CanExecute(object parameter)
        {
            Validator.AssignableFrom<T>(parameter);
            return this.CanExecute((T) parameter);
        }

        /// <summary>
        /// </summary>
        /// <param name="parameter">
        /// </param>
        void ICommand.Execute(object parameter)
        {
            Validator.AssignableFrom<T>(parameter);
            this.Execute((T) parameter);
        }

        /// <summary>
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Raise when property of command changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raise for to check if the command can execute.
        /// </summary>
        /// <param name="parameter">
        /// data, witch uses the command
        /// </param>
        /// <returns>
        /// return true if can execute
        /// </returns>
        public bool CanExecute(T parameter)
        {
            bool result = true;
            if (this.canExecuteMethod != null)
            {
                result = this.canExecuteMethod(parameter);
            }

            return result;
        }

        /// <summary>
        /// </summary>
        /// <param name="parameter">
        /// data, witch uses the command
        /// </param>
        public void Execute(T parameter)
        {
            if (this.executeMethod != null)
            {
                this.executeMethod(parameter);
            }
        }

        /// <summary>
        /// Raise for to check if the command can execute.
        /// </summary>
        public void DoCanExecuteChanged()
        {
            this.OnCanExecuteChanged();
        }

        /// <summary>
        /// Raise for to check if the command can execute.
        /// </summary>
        protected virtual void OnCanExecuteChanged()
        {
            EventHandler canExecuteChangedHandler = this.CanExecuteChanged;
            if (canExecuteChangedHandler != null)
            {
                canExecuteChangedHandler(this, EventArgs.Empty);
            }
        }
    }
}